package cc.yugu.yhdw.common.domain.hdw;

import cc.yugu.yhdw.common.domain.PageEntity;
import cc.yugu.yhdw.common.entity.json.DateTimeSerializer;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class BatteryReportLog extends PageEntity {

    public enum NetworkType {
        NETWORK_0(0, "GSM/GPRS"),
        NETWORK_1(1, "Wired"),
        NETWORK_2(2, "3G"),
        NETWORK_3(3, "4G");

        private final int value;
        private final String name;
        private static final Map<Integer, String> map = new HashMap<Integer, String>();

        static {
            for (NetworkType e : NetworkType.values()) {
                map.put(e.getValue(), e.getName());
            }
        }

        NetworkType(int value, String name) {
            this.value = value;
            this.name = name;
        }

        public int getValue() {
            return value;
        }

        public String getName() {
            return name;
        }

        public static String getName(int value) {
            return map.get(value);
        }
    }

    //0充电  1放电  2不充电也不放电
    public enum FetStatus {
        CHARGE(0, "充电"),
        DISCHARGE(1, "放电"),
        STATIC(2, "不充电也不放电");

        private final int value;
        private final String name;

        FetStatus(int value, String name) {
            this.value = value;
            this.name = name;
        }

        private static Map<Integer, String> map = new HashMap<Integer, String>();

        static {
            for (FetStatus e : FetStatus.values()) {
                map.put(e.getValue(), e.getName());
            }
        }

        public static String getName(int value) {
            return map.get(value);
        }

        public int getValue() {
            return value;
        }

        public String getName() {
            return name;
        }
    }

    public enum ProtectionStatus {
        CODE_1(1, "单体过压发生保护"),
        CODE_2(2, "单体欠压发生保护"),
        CODE_3(3, "整组过压发生保护"),
        CODE_4(4, "整组欠压发生保护"),
        CODE_5(5, "充电过温发生保护"),
        CODE_6(6, "充电低温发生保护"),
        CODE_7(7, "放电过温发生保护"),
        CODE_8(8, "放电低温发生保护"),
        CODE_9(9, "充电过流发生保护"),
        CODE_10(10, "放电过流发生保护"),
        CODE_11(11, "短路发生保护"),
        CODE_12(12, "前端检测IC错误"),
        CODE_13(13, "软件锁定MOS"),
        CODE_14(14, ""),
        CODE_15(15, ""),
        CODE_16(16, ""),;

        private final int value;
        private final String name;

        ProtectionStatus(int value, String name) {
            this.value = value;
            this.name = name;
        }

        private static Map<Integer, String> map = new HashMap<Integer, String>();

        static {
            for (ProtectionStatus e : ProtectionStatus.values()) {
                map.put(e.getValue(), e.getName());
            }
        }

        public static String getName(int value) {
            return map.get(value);
        }

        public int getValue() {
            return value;
        }

        public String getName() {
            return name;
        }
    }

    public String batteryId;
    public String bicycleNo; //
    public Integer protectionStatus; //
    public String ntcTemperature; //
    public Integer batteryVolume; //
    public Double lng; //
    public Double lat; //
    public Double electricity; //
    public Double speed; //
    public Integer currentSignal;/*信号*/
    public Integer networkType;/**/
    public Integer voltage; //
    public Integer electricCurrent; //
    public Integer fetStatus; //
    public String simCode; //
    public String version;
    public Integer reportInterval; //
    public Integer distance;//上传间隔公里
    public Integer dumpEnergy;//
    public String orderId; //
    public String coordinateType; //
    public String address; //
    public Date createTime; //

    String suffix;

    public BatteryReportLog() {
    }

    public BatteryReportLog(String bicycleNo, String batteryId, String batteryVolume, Double lng, Double lat, String speed, String reportInterval, String distance,
                            String electricity, String dumpEnergy, String protectionStatus, String ntcTemperature, String coordinateType) {
        this.batteryId = batteryId;
        this.bicycleNo = bicycleNo;
        if (StringUtils.isNotEmpty(batteryVolume)) {
            this.batteryVolume = Integer.valueOf(batteryVolume);
        }
        this.lng = lng;
        this.lat = lat;
        if (StringUtils.isNotEmpty(speed)) {
            this.speed = Double.valueOf(speed);
        }
        if (StringUtils.isNotEmpty(reportInterval)) {
            this.reportInterval = Integer.valueOf(reportInterval);
        }
        if (StringUtils.isNotEmpty(distance)) {
            this.distance = Integer.valueOf(distance);
        }
        if (StringUtils.isNotEmpty(electricity)) {
            this.electricity = Double.valueOf(electricity);
        }
        if (StringUtils.isNotEmpty(dumpEnergy)) {
            this.dumpEnergy = Integer.valueOf(dumpEnergy);
        }
        if (StringUtils.isNotEmpty(protectionStatus)) {
            this.protectionStatus = Integer.valueOf(protectionStatus);
        }
        if (StringUtils.isNotEmpty(ntcTemperature)) {
            this.ntcTemperature = ntcTemperature;
        }
        this.coordinateType = coordinateType;

    }

    public BatteryReportLog(String bicycleNo, String batteryId, String batteryVolume, Double lng, Double lat, String speed, String reportInterval, String distance,
                            String electricity, String dumpEnergy, String protectionStatus, String ntcTemperature, String networkType, String currentSignal,
                            String voltage, String electricCurrent, String simCode, String fetStatus, String version, String coordinateType) {
        this.batteryId = batteryId;
        this.bicycleNo = bicycleNo;
        if (StringUtils.isNotEmpty(batteryVolume)) {
            this.batteryVolume = Integer.parseInt(batteryVolume, 16);
        }
        this.lng = lng;
        this.lat = lat;
        // 已注册电池上报日志-速度过滤条件--不是gps过滤掉
        if (StringUtils.isNotEmpty(speed)) {
            /*if (coordinateType != null && "gps".equals(coordinateType)) {
                this.speed = Double.valueOf(speed);
            }*/
            this.speed = Double.valueOf(speed);
        }
        if (StringUtils.isNotEmpty(reportInterval)) {
            this.reportInterval = Integer.valueOf(reportInterval);
        }
        if (StringUtils.isNotEmpty(distance)) {
            this.distance = Integer.parseInt(distance, 16);
        }
        if (StringUtils.isNotEmpty(electricity)) {
            this.electricity = Double.valueOf(Integer.parseInt(electricity, 16));
        }
        if (StringUtils.isNotEmpty(dumpEnergy)) {
            this.dumpEnergy = Integer.parseInt(dumpEnergy, 16);
        }
        if (StringUtils.isNotEmpty(protectionStatus)) {
            this.protectionStatus = Integer.parseInt(protectionStatus, 16);
        }
        if (StringUtils.isNotEmpty(ntcTemperature)) {
            String[] list = StringUtils.split(ntcTemperature, ", ");
            String t = null;
            for (String str : list) {
                if (t == null) {
                    t = String.valueOf((Integer.parseInt(str, 16) - 2731) / 10);
                } else {
                    t += "," + (Integer.parseInt(str, 16) - 2731) / 10;
                }
            }
            this.ntcTemperature = t;
        }
        if (StringUtils.isNotEmpty(networkType)) {
            this.networkType = (Integer.valueOf(networkType));
        }
        if (StringUtils.isNotEmpty(currentSignal)) {
            this.currentSignal = (Integer.valueOf(currentSignal));
        }
        if (StringUtils.isNotEmpty(voltage)) {
            this.voltage = Integer.parseInt(voltage, 16);
        }
        if (StringUtils.isNotEmpty(electricCurrent)) {
            Integer e = Integer.parseInt(electricCurrent, 16);
            if (e == null || e == 0) {
                this.electricCurrent = 0;
            } else {
                this.electricCurrent = 65535 - e;
            }
        }
        this.simCode = simCode;
        if (StringUtils.isNotEmpty(fetStatus)) {
            this.fetStatus = Integer.parseInt(fetStatus, 16);
        }
        this.version = version;
        this.coordinateType = coordinateType;
    }

    public BatteryReportLog(String bicycleNo, String batteryId, Integer batteryVolume, Double lng, Double lat,
                            String dumpEnergy, String protectionStatus, String ntcTemperature,
                            Integer voltage, Integer electricCurrent, Integer fetStatus) {
        this.bicycleNo = bicycleNo;
        this.batteryId = batteryId;
        this.batteryVolume = batteryVolume;
        this.lng = lng;
        this.lat = lat;
        if (StringUtils.isNotEmpty(dumpEnergy)) {
            this.dumpEnergy = Integer.valueOf(dumpEnergy);
        }
        this.protectionStatus = Integer.parseInt(protectionStatus, 16);

        if (StringUtils.isNotEmpty(ntcTemperature)) {
            this.ntcTemperature = ntcTemperature;
        }
        this.voltage = voltage;
        this.electricCurrent = electricCurrent;
        this.fetStatus = fetStatus;

    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCoordinateType() {
        return coordinateType;
    }

    public void setCoordinateType(String coordinateType) {
        this.coordinateType = coordinateType;
    }

    public Integer getNetworkType() {
        return networkType;
    }

    public void setNetworkType(Integer networkType) {
        this.networkType = networkType;
    }

    public Integer getVoltage() {
        return voltage;
    }

    public void setVoltage(Integer voltage) {
        this.voltage = voltage;
    }

    public Integer getElectricCurrent() {
        return electricCurrent;
    }

    public void setElectricCurrent(Integer electricCurrent) {
        this.electricCurrent = electricCurrent;
    }

    public Integer getFetStatus() {
        return fetStatus;
    }

    public void setFetStatus(Integer fetStatus) {
        this.fetStatus = fetStatus;
    }

    public String getSimCode() {
        return simCode;
    }

    public void setSimCode(String simCode) {
        this.simCode = simCode;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public Integer getCurrentSignal() {
        return currentSignal;
    }

    public void setCurrentSignal(Integer currentSignal) {
        this.currentSignal = currentSignal;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getBatteryId() {
        return batteryId;
    }

    public void setBatteryId(String batteryId) {
        this.batteryId = batteryId;
    }

    public Integer getProtectionStatus() {
        return protectionStatus;
    }

    public void setProtectionStatus(Integer protectionStatus) {
        this.protectionStatus = protectionStatus;
    }

    public String getNtcTemperature() {
        return ntcTemperature;
    }

    public void setNtcTemperature(String ntcTemperature) {
        this.ntcTemperature = ntcTemperature;
    }

    public Double getElectricity() {
        return electricity;
    }

    public void setElectricity(Double electricity) {
        this.electricity = electricity;
    }

    public Integer getDumpEnergy() {
        return dumpEnergy;
    }

    public void setDumpEnergy(Integer dumpEnergy) {
        this.dumpEnergy = dumpEnergy;
    }

    public String getBicycleNo() {
        return bicycleNo;
    }

    public void setBicycleNo(String bicycleNo) {
        this.bicycleNo = bicycleNo;
    }

    public Integer getBatteryVolume() {
        return batteryVolume;
    }

    public void setBatteryVolume(Integer batteryVolume) {
        this.batteryVolume = batteryVolume;
    }

    public Double getLng() {
        return lng;
    }

    public void setLng(Double lng) {
        this.lng = lng;
    }

    public Double getLat() {
        return lat;
    }

    public void setLat(Double lat) {
        this.lat = lat;
    }

    public Double getSpeed() {
        return speed;
    }

    public void setSpeed(Double speed) {
        this.speed = speed;
    }

    public Integer getDistance() {
        return distance;
    }

    public void setDistance(Integer distance) {
        this.distance = distance;
    }

    public Integer getReportInterval() {
        return reportInterval;
    }

    public void setReportInterval(Integer reportInterval) {
        this.reportInterval = reportInterval;
    }

    @JsonSerialize(using = DateTimeSerializer.class)
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getNetworkTypeName() {
        if (this.networkType != null) {
            return NetworkType.getName(this.networkType);
        }
        return "";
    }

    public String getFetStatusName() {
        if (this.fetStatus != null) {
            return FetStatus.getName(this.fetStatus);
        }
        return "";
    }

    public String getProtectionStatusName() {
        String name = "";
        if (null != protectionStatus) {
            ProtectionStatus[] names = ProtectionStatus.values();
            char[] str = new StringBuilder(String.format("%0" + names.length + "d", Long.parseLong(Integer.toBinaryString(protectionStatus)))).reverse().toString().toCharArray();

            for (int i = 0; i < str.length; i++) {
                if ('1' == str[i]) {
                    if (StringUtils.isNotEmpty(name)) {
                        name += ",";
                    }
                    name += names[i].name;
                }
            }
        }
        return name;
    }
}
